package com.foreknow.dao.impl;

import com.foreknow.dao.IBaseDao;
import com.foreknow.entity.Reply;
import com.foreknow.util.DbHelper;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ReplyDaoImpl implements IBaseDao<Reply> {
	private static QueryRunner queryRunner = DbHelper.getQueryRunner();

	@Override
	public int delete(String sql, Object... params) throws SQLException {
		return queryRunner.update(sql, params);
	}

	@Override
	public int delete(String sql) throws SQLException {
		return queryRunner.update(sql);

	}

	@Override
	public List<Reply> queryByCondition(String sql) throws SQLException {
		return queryRunner.query(sql, getListHandler());

	}

	@Override
	public int update(String sql, Object... params) throws SQLException {
		return queryRunner.update(sql, params);

	}

	@Override
	public int update(String sql, Object param) throws SQLException {
		return queryRunner.update(sql, param);
	}

	@SuppressWarnings("deprecation")
	@Override
	public Reply queryOne(String sql, Object param) throws SQLException {
		return queryRunner.query(sql, param, getResultSetHandler());

	}

	@SuppressWarnings("deprecation")
	public Reply queryOne(String sql, Object[] params) throws SQLException {
		return queryRunner.query(sql, params, getResultSetHandler());
	}

	@Override
	public ResultSetHandler<Reply> getResultSetHandler() {
		ResultSetHandler<Reply> handler = new ResultSetHandler<Reply>() {
			@Override
			public Reply handle(ResultSet resultSet) throws SQLException {
				Reply reply = null;
				while (resultSet.next()) {
					Integer id = resultSet.getInt("id");
					Integer blogId = resultSet.getInt("blogId");
					Integer replyId = resultSet.getInt("replyId");
					Integer authorId = resultSet.getInt("authorId");
					Integer answererId = resultSet.getInt("answererId");
					String content = resultSet.getString("content");
					String uploadTime = resultSet.getString("uploadTime");
					reply = new Reply(id, blogId, replyId, authorId, answererId, content, uploadTime);
				}
				return reply;
			}
		};
		return handler;
	}

	public ResultSetHandler<List<Reply>> getListHandler() {
		ResultSetHandler<List<Reply>> handler = new ResultSetHandler<List<Reply>>() {
			@Override
			public List<Reply> handle(ResultSet resultSet) throws SQLException {
				List<Reply> replies = new ArrayList<>();
				while (resultSet.next()) {
					Integer id = resultSet.getInt("id");
					Integer blogId = resultSet.getInt("blogId");
					Integer replyId = resultSet.getInt("replyId");
					Integer authorId = resultSet.getInt("authorId");
					Integer answererId = resultSet.getInt("answererId");
					String content = resultSet.getString("content");
					String uploadTime = resultSet.getString("uploadTime");
					Reply reply = new Reply(id, blogId, replyId, authorId, answererId, content, uploadTime);
					replies.add(reply);
				}
				return replies;
			}
		};
		return handler;
	}

	@Override
	public int insert(String sql, Object[] params) throws SQLException {
		return queryRunner.update(sql, params);

	}

	@Override
	public List<Reply> queryByCondition(String sql, Object... params) throws SQLException {
		return queryRunner.query(sql, getListHandler(),params);
	}
}
